package com.itlaobing.data;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class DataInputTest2 {

    public static void main(String[] args) throws IOException {
        Charset charset = Charset.forName( "UTF-8" );// 根据字符编码方案的名称获取charset实例

        InputStream in = new FileInputStream( "D:/student.data" );
        DataInputStream dis = new DataInputStream( in );

        int id = dis.readInt() ; // 从输入流中读取 4 个字节并重构为 int 类型的数值
        System.out.println( id );

        byte[] bytes = new byte[ 32 ];
        dis.read( bytes ); // 从输入流中读取 32 个字节并将这些字节存放到 bytes 数组中

        // 根据数组中的 字节序列 按照指定的 charset 构造一个 String实例 ( 字符序列 )
        String name = new String( bytes , charset );
        name = name.trim() ; // 剔除首尾空白
        System.out.println( name );

        char gender = dis.readChar();
        System.out.println( gender );

        short age = dis.readShort();
        System.out.println( age );

        //System.out.println(id + "," + name + "," + gender + "," + age);
        dis.close();
        in.close();
    }
}
